<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8"/>
    <title>用户角色列表</title>

    <!-- CSS -->
    <link rel="stylesheet" th:href="@{static/plugins/layui/css/layui.css}"/>
</head>
<body>

<div style="margin:10px;">
    <form class="layui-form">

        <div th:each="item,iterStat : ${sysRoles}">
            <div class="layui-form-item">
                <label class="layui-form-label" th:text="${item.sr01}"></label>
                <div class="layui-input-block">
                    <!--<input type="checkbox" name="role" th:value="${item.sr02}" lay-skin="switch" lay-text="开启|关闭">-->
                    <input type="checkbox" name="role" th:value="${item.sr02}" th:checked="${#strings.contains(userRoleS,item.sr02) ? true :false}" lay-skin="switch" lay-text="开启|关闭">
                </div>
            </div>
        </div>
        <input type="hidden" class="layui-input" name="su00" th:value="${su00}"/>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="addUserFilter">保存</button>
            </div>
        </div>
    </form>
</div>

<!-- JS -->
<script type="text/javascript" th:src="@{static/plugins/layui/layui.js}"></script>

<script th:inline="javascript">

    layui.use(['form', 'layer','table','laydate','upload'], function () {
        var $ = layui.jquery;
        var form = layui.form;
        var layer = layui.layer;

        form.render();

        //保存信息
        form.on('submit(addUserFilter)', function (data) {

            var checkbox_value = [];
            $('input[name="role"]:checked').each(function () {
                checkbox_value.push($(this).val());
            });
            if(!checkbox_value.join(',')){
                layer.msg("请至少选择一个角色",{icon:5,time:2000});
            }else{
                data.field["temp1"] = checkbox_value.join(',');
                $.ajax({
                    url: 'sysUser/addSysUserRole',
                    type: 'post',
                    data: {jsonStr:JSON.stringify(data.field)},
                    dataType: 'json',
                    success: function (res) {
                        if(0 === res.code){
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.msg(res.msg,{icon:6,time: 1500});
                            parent.layui.table.reload('user_table_id');
                            parent.layer.close(index);
                        }else{
                            layer.msg(res.msg, {icon: 5, time: 1500});
                        }
                    }
                });
            }
            return false;
        });

    });
</script>

</body>
</html>